/*4X4Matrix Keyboard*/
#include "4X4MatrixKeyboard.h"
void MatrixKeyboardDelay(unsigned int i)
{
	while(i--);	
}

//键盘按键按下（返回模式）
//BackMode为0则返回字符
//BackMode为1则返回十六进制
char KeyboardKeyDown(char BackMode)
{
	unsigned int KeyValue,js=0;
	GPIO_KEY=0x0f;
	if(GPIO_KEY!=0x0f)
	{
		MatrixKeyboardDelay(500);
		if(GPIO_KEY!=0x0f)
		{
			GPIO_KEY=0x0f;
			switch(GPIO_KEY)
			{
				case(0x07):KeyValue=0;break;
				case(0x0b):KeyValue=1;break;
				case(0x0d):KeyValue=2;break;
				case(0x0e):KeyValue=3;break;
			}
			GPIO_KEY=0xf0;
			switch(GPIO_KEY)
			{
				case(0x70):KeyValue+=0;break;
				case(0xb0):KeyValue+=4;break;
				case(0xd0):KeyValue+=8;break;
				case(0xe0):KeyValue+=12;break;
			}
			while((js<50)&&(GPIO_KEY!=0xf0))
			{
				MatrixKeyboardDelay(1000);
				js++;
			}
			switch(KeyValue)
			{
				case(0):if(BackMode==0){return '1';}else{return 0x01;}
				case(1):if(BackMode==0){return '2';}else{return 0x02;}
				case(2):if(BackMode==0){return '3';}else{return 0x03;}
				case(3):if(BackMode==0){return 'a';}else{return 0x0a;}
				case(4):if(BackMode==0){return '4';}else{return 0x04;}
				case(5):if(BackMode==0){return '5';}else{return 0x05;}
				case(6):if(BackMode==0){return '6';}else{return 0x06;}
				case(7):if(BackMode==0){return 'b';}else{return 0x0b;}
				case(8):if(BackMode==0){return '7';}else{return 0x07;}
				case(9):if(BackMode==0){return '8';}else{return 0x08;}
				case(10):if(BackMode==0){return '9';}else{return 0x09;}
				case(11):if(BackMode==0){return 'c';}else{return 0x0c;}
				case(12):if(BackMode==0){return 'e';}else{return 0x0e;}
				case(13):if(BackMode==0){return '0';}else{return 0x00;}
				case(14):if(BackMode==0){return 'f';}else{return 0x0f;}
				case(15):if(BackMode==0){return 'd';}else{return 0x0d;}
			}
		}
	}
	return -1;
}
